package com.cinsea.ext.xyc;

import com.cinsea.common.ActionType;
import com.cinsea.common.BasePX;
import com.cinsea.common.Env;
import com.cinsea.common.JdbcDao;
import com.cinsea.htmlselect.service.OptionService;
import com.cinsea.interfaces.form.Formdata;
import com.cinsea.interfaces.form.FormdataServiceImpl;
import com.cinsea.interfaces.model.Cell;
import com.cinsea.interfaces.model.Dataset;
import com.cinsea.interfaces.workflow.RequestInfo;
import com.cinsea.util.DateUtil;
import com.cinsea.util.NumberUtil;
import com.cinsea.util.StringUtil;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: Administrator
 * Date: 13-12-29
 * Time: 下午1:07
 * To change this template use File | Settings | File Templates.
 */
public class ReducePx extends BasePX {
    @Override
    public void before(Map context) {
        JdbcDao jdbcDao = Env.getBean("jdbcDao");
        String actiontype = StringUtil.null2String(context.get("actiontype"));
        Map m=(Map)context.get("parameters");
        String bwid = StringUtil.null2String(m.get("ut_reduceplant_baiwei"));
        String lzid = StringUtil.null2String(m.get("ut_reduceplant_lvzhi"));
        String bfsl = StringUtil.null2String(m.get("ut_reduceplant_bfsl"),"0");
        String hhsl = StringUtil.null2String(m.get("ut_reduceplant_amount"),"0");
        String apper = StringUtil.null2String(m.get("ut_reduceplant_shenqingren"),Env.getRemoteUser().getId());
        String appdate = StringUtil.null2String(m.get("ut_reduceplant_shenqingriqi"), DateUtil.getCurrentDate());
        String apptime = StringUtil.null2String(m.get("ut_reduceplant_sqsj"),DateUtil.getCurrentTime());
        
        if(actiontype.equals(ActionType.SUBMIT_WFPROCESS)){
            OptionService optionService = Env.getBean("optionService");
            Map lvzhidata = jdbcDao.queryForMap("select guigexinghao,chmc,chdlmc from ut_plantinfo where processid=?", new Object[]{lzid});
            String chdlmc =StringUtil.null2String(lvzhidata.get("chdlmc"));
            String sql = "";
            String sq = "";
            if(chdlmc.equals("402881923ff47408013ff54243eb0027")){
                sql = "update ut_postplant set hjsl =? , yzzj =? where baiwei =? and huajia =?";
                sq = "select processid,yzdj from ut_postplant where baiwei =? and huajia = ?";
            }
            else if (chdlmc.equals("402881923ff47408013ff54281b50028")){
                sql = "update ut_postplant set hpsl =? , yzzj =? where baiwei =? and huapen =?";
                sq = "select processid,yzdj from ut_postplant where baiwei =? and huapen = ?";
            }
            else{
                sql = "update ut_postplant set bfsl =? , yzzj =? where baiwei =? and lvzhi =?";
                sq = "select processid,yzdj from ut_postplant where baiwei =? and lvzhi = ?";
            }


            List<Map> list =(List<Map>) jdbcDao.queryForList(sq,new Object[]{bwid,lzid});
            if(list.size()>0){
                Map postplant = list.get(0);
               /* Float oldyzdj = Float.parseFloat(StringUtil.null2String(postplant.get("yzdj"),"0"));
                Float yzzj = 0f;
                yzzj = oldyzdj*num;
                jdbcDao.update(sql,new Object[]{num,yzzj,bwid,lzid});*/
                FormdataServiceImpl si = new FormdataServiceImpl();
                Formdata formdata = new RequestInfo();
                formdata.setOperator(Env.getRemoteUser().getId());
                formdata.setTypeid("402881934060ce6c01406194917b000d"); //换花申请目录id
                Dataset ds = new Dataset();
                List<Cell> mainlist = new ArrayList(); //主表单元格
                Cell c1 = new Cell();
                c1.setId("ut_changeplant_baiwei");
                c1.setValue(bwid);
                Cell c2 = new Cell();
                c2.setId("ut_changeplant_lvzhi");
                c2.setValue(lzid);

                Cell c4 = new Cell();
                c4.setId("ut_changeplant_applier");
                c4.setValue(apper);
                Cell c5 = new Cell();
                c5.setId("ut_changeplant_appdate");
                c5.setValue(appdate);
                Cell c6 = new Cell();
                c6.setId("ut_changeplant_apptime");
                c6.setValue(apptime);
                String type ="402881ee42b153ca0142da4e563313a9";//减花
                Cell c7 = new Cell();
                c7.setId("ut_changeplant_hhlx");
                c7.setValue(type);
                Cell c8 = new Cell();
                c8.setId("ut_changeplant_hhzt");
                c8.setValue("402881934061fddd0140622fbc2a0009");//已申请换花
                Cell c9 = new Cell();
                c9.setId("ut_changeplant_bwlzid");
                c9.setValue(postplant.get("processid").toString());
                Map baiweidata = jdbcDao.queryForMap("select lcmc from ut_floorinfo where processid=?", new Object[]{bwid});
                Cell c10 = new Cell();
                c10.setId("ut_changeplant_bwmc");
                c10.setValue(StringUtil.null2String(baiweidata.get("lcmc")));

                Cell c12 = new Cell();
                c12.setId("ut_changeplant_guigexinghao");
                c12.setValue(StringUtil.null2String(optionService.get(StringUtil.null2String(lvzhidata.get("guigexinghao"))).getObjname()));
                Cell c13 = new Cell();
                c13.setId("ut_changeplant_lzmc");
                c13.setValue(StringUtil.null2String(lvzhidata.get("chmc")));
                Cell c14 = new Cell();
                c14.setId("ut_changeplant_lzlx");
                c14.setValue(StringUtil.null2String(optionService.get(StringUtil.null2String(lvzhidata.get("chdlmc"))).getObjname()));
                Cell c15 = new Cell();
                c15.setId("ut_changeplant_lzsl");
                c15.setValue(bfsl);
                Cell c16 = new Cell();
                c16.setId("ut_changeplant_hhsl");
                c16.setValue(hhsl);
                mainlist.add(c1);
                mainlist.add(c2);
                mainlist.add(c4);
                mainlist.add(c5);
                mainlist.add(c6);
                mainlist.add(c7);
                mainlist.add(c8);
                mainlist.add(c9);
                mainlist.add(c10);
                mainlist.add(c12);
                mainlist.add(c13);
                mainlist.add(c14);
                mainlist.add(c15);
                mainlist.add(c16);
                ds.setMaintable(mainlist);
                formdata.setData(ds);
                si.createFormdata(formdata);
            }
        }
    }
}
